<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class Payrule extends Base
{
    public function index(){
        if(input('?param.status')){
            $status = input('status');
        }else{
            $status = input('status')?:'all';
        }
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            $count = Db::name('payrule')->where($map)->count();
            $field = '*';
            $list = Db::name('payrule')
                ->field($field)
                ->where($map)
                ->order('id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        return View::fetch();
    }
    public function edit(){
        $id = input('id');
        $info = Db::name('payrule')->where('id',$id)->find();
        //钱包
        View::assign('wallet',logic('Wallet')->getName());
        if($info){
            if($info['wallet']) $info['wallet'] = unserialize($info['wallet']);
        }
        View::assign('info',$info);
        return View::fetch();
    }

    public function save(){
        $data = input('info');
        if($data['name']=='' || $data['biaoming']=='' || $data['tiaojian']=='' || $data['ziduan']==''){
            return json(['status'=>0,'msg'=>'请填写必填项']);
        }
        //判断标题是否存在
        $map['name'] = $data['name'];
        if($data['id']){
            $map['id']=['<>',$data['id']];
        }
        $have = Db::name('payrule')->where($map)->find();
        if($have && $have['id']!=$data['id']){
            return json(['status'=>0,'msg'=>'标题已经存在']);
        }

        //判断抵扣是否存在
        if($data['dikou']==1){
            if(empty($data['dikou_id'])){
                return json(['status'=>0,'msg'=>'请输入抵扣关联ID']);
            }else{
                $have = Db::name('Payrule')->find($data['dikou_id']);
                if(!$have){
                    return json(['status'=>0,'msg'=>'抵扣关联ID不存在']);
                }
            }
        }
        $input_wallet = input('wallet/a');
        $walletpx = input('walletpx/a');
        if(!isset($input_wallet) && !isset($input_wallet)){
            return json(['status'=>0,'msg'=>'请选择支付方式']);
        }
        if(isset($input_wallet)){
            $wallet = [];
            foreach($input_wallet as $key=>$val){
                $wallet[$val] = $walletpx[$key];
            }
            $data['wallet'] = serialize($wallet);
        }else{
            $data['wallet'] = '';
        }
        $data['w_time'] = time();
        unset($data['walletpx']);
        if($data['id']){
            $id = $data['id'];
            $res = Db::name('payrule')->where('id',$id)->update($data);
        }else{
            $res = Db::name('payrule')->insert($data);
        }
        return json(['status'=>1,'msg'=>'操作成功！']);
    }
}